var app=getApp();
var ux = require('../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {   
    thisId:null,
    thisIndex:null,
    orderJd:false,
    orderJj:false,
    orderTk:false,
    orderTh:false,
    open:true,
    distributor:{},
    tab:0, 
    list: [[], [], [], [], []],
    pages: [0, 0, 0, 0, 0],
    nexts: [true, true, true, true, true],
    size: 10,
    winheight: 0,
    winwidth: 0,
    subUrl: "/api/delivery/saoHeOrder.do",
    tkUrl:"/api/delivery/disAgreeTorefund.do",
    jjUrl: "/api/delivery/ditrutorRefundment.do",
    jdUrl: "/api/delivery/agreeReceiptOrders.do",
    thUrl: "/api/delivery/returnHead.do",
    action: "/api/delivery/deliveryOrders.do",
	submit:"{{submit}}",
  isShow:true,
  },
  loadMore: function () {
    this.getList();
  },
  getList: function () {
    var self = this;

    if (self.data.nexts[this.data.tab] == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = {type: this.data.tab, pageNo: parseInt(self.data.pages[this.data.tab]), pageSize: self.data.size };
    ux.request(self.data.action, params, function (data) {
      console.log(JSON.stringify(data.data.list));
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        var nexts = self.data.nexts;
        nexts[self.data.tab] = true;
        self.setData({
          nexts: nexts
        });
      } else {
        var nexts = self.data.nexts;
        nexts[self.data.tab] = false;
        self.setData({
          nexts: nexts
        });
      }
      if (self.data.pages[self.data.tab] == 0) {
        var lists = self.data.list;
        lists[self.data.tab] = datas;
        self.setData({
          list: lists
        });
      } else {
        var lists = self.data.list;
        lists[self.data.tab] = lists[self.data.tab].concat(datas);
        self.setData({
          list: lists
        });
      }
      var pages = self.data.pages;
      pages[self.data.tab] = pages[self.data.tab] + 1;
      self.setData({
        pages: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    }, true);
  },
  onBar: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val + "" != "") {
      if (dataset.val != this.data.tab) {
          data[dataset.key] = dataset.val;
          var lists=this.data.list;
          lists[this.data.tab]=[];
        var pagesa = this.data.pages;
        pagesa[this.data.tab] = 0;
        var nextsa = this.data.nexts;
        nextsa[this.data.tab] = true;
          this.setData({
            list: lists,
            pages: pagesa,
            nexts: nextsa
          })
          this.getList();
      } else {
        data[dataset.key] = dataset.val;
      }

    }
    this.setData(data);
  },
  preventTouchMoveTy:function(){
    return false;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      distributor: wx.getStorageSync("distributor")
    });
    var self = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth;
        self.setData({
          winheight: clientHeight,
          winwidth: clientWidth
        });
      }
    });
  },
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
    if (dataset.key && dataset.val + "" != ""){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.isShow){
           this.setData({
             orderJd: false,
             orderJj: false,
             orderTk: false,
             orderTh: false,
             tab: 0,
             list: [[], [], [], [], []],
             pages: [0, 0, 0, 0, 0],
             nexts: [true, true, true, true, true],
             isShow: false
           })
         this.getList();    
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  toTab3:function(){
    wx.showLoading({
      title: '加载中....',
    })
    wx.redirectTo({
      url: '/pages/distributor/tab3/myFinance',
    })
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  tuikuanliyouo:function(e){
     this.setData({
       tuikuanliyouo:e.detail.value
     })
  },
  openorderJj:function(event){//打开拒绝弹窗
    var dataset = event.currentTarget.dataset;
    this.setData({
      orderJj: true,
      thisId: dataset.id,
      thisIndex: dataset.index,
    })
  },
  openorderTh: function (event) {//打开退回总部弹窗
    var dataset = event.currentTarget.dataset;
    this.setData({
      orderTh: true,
      thisId: dataset.id,
      thisIndex: dataset.index,
    })
  },
  openorderJd: function (event) {//打开同意接单弹窗
    var dataset = event.currentTarget.dataset;
    this.setData({
      orderJd: true,
      thisId: dataset.id,
      thisIndex: dataset.index,
    })
  },
  openorderTk: function (event) {//打开同意退款弹窗
    var dataset = event.currentTarget.dataset;
    this.setData({
      orderTk: true,
      thisId: dataset.id,
      thisIndex: dataset.index,
    })
  },
  orderJjX: function () {//关闭拒绝弹窗
    this.setData({
      orderJj: false,
    })
  },
  orderThX: function () {//关闭退回总部弹窗
    this.setData({
      orderTh: false,
    })
  },
  orderJdX: function () {//关闭同意接单弹窗
    this.setData({
      orderJd: false,
    })
  },
  orderTkX: function () {//关闭同意退款弹窗
     this.setData({
       orderTk: false,
     })
  },
  orderJjR: function () {//拒绝弹窗按确定按钮
    if (!this.data.tuikuanliyouo) {
      this.toast("请输入决绝理由");
      return false;
    }
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    
    var self = this;
    var parmas = { orderId: this.data.thisId, content: this.data.tuikuanliyouo};
    ux.request(this.data.jjUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          orderJj: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          orderJj: false,
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  orderThR: function () {//退回总部弹窗按确定按钮
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var self = this;
    var parmas = { orderId: this.data.thisId };
    ux.request(this.data.thUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          orderTh: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          orderTh: false,
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  orderJdR: function () {//同意接单弹窗按确定按钮
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var self = this;
    var parmas = { orderId: this.data.thisId };
    ux.request(this.data.jdUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          orderJd: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          orderJd: false,
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  orderTkR: function () {//同意退款弹窗按确定按钮
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var self = this;
    var parmas = { orderId: this.data.thisId };
    ux.request(this.data.tkUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          orderTk: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          orderTk: false,
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  saoyisao: function () {
    var self = this;
    wx.scanCode({
      success: (res) => {
        wx.showLoading({
          title: '处理中...',
          mask: true
        })
        var pamars = { result: res.result }
        ux.request(this.data.subUrl, pamars, function (protocol) {

          if (protocol.data.type == 1) {
            self.toast("核销成功");
            self.setData({
              isShow: true
            })
            self.onShow();
          } else {
            self.toast("核销失败");
          }
        }, true);
        setTimeout(function () { wx.hideLoading(); }, 1000);

      },
      fail: (res) => {
        self.toast("扫码失败");
      }
    })
  },
  gotoDao:function(event){
    var dataset = event.currentTarget.dataset;
    var item = this.data.list[this.data.tab][dataset.index];
  
  wx.getSetting({
    success: (res) => {
      if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
        wx.showModal({
          title: '是否授权当前位置',
          content: '需要获取您的地理位置，请确认授权，否则地图定位功能将无法使用',
          success: function (res) {
            if (res.cancel) {
              console.info("1授权失败返回数据");

            } else if (res.confirm) {
              //village_LBS(that);
              wx.openSetting({
                success: function (data) {
                  if (data.authSetting["scope.userLocation"] == true) {
                    wx.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 5000
                    })
                    wx.openLocation({
                      latitude: parseFloat(item.latitude),
                      longitude: parseFloat(item.longitude),
                      name: item.address,
                      scale: 28
                    })
                  } else {
                    wx.showToast({
                      title: '授权失败',
                      icon: 'success',
                      duration: 5000
                    })
                  }
                }
              })
            }
          }
        })
      } else {
        wx.openLocation({
          latitude: parseFloat(item.latitude),
          longitude: parseFloat(item.longitude),
          name: item.address,
          scale: 28
        })
      }
    }
  })
  }
})
